package org.rrs.duker.ui.fragment;

import android.content.Context;
import android.os.Bundle;
import android.app.Fragment;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import org.rrs.duker.R;
import org.rrs.duker.pojo.SummaryInfo;
import org.rrs.duker.ui.DividerItemDecoration;
import org.rrs.duker.ui.NormalRecyclerViewAdapter;
import org.rrs.duker.api.LocalApiHelper;
import org.rrs.duker.ui.activity.HomeActivity;

import java.util.List;

/**
 * 订阅源对应的SummaryInfoList显示的fragment
 * 用来显示订阅源的摘要list
 */
public class SumInfoListFragment extends Fragment {

    private final static String TAG = SumInfoListFragment.class.getSimpleName();
    private final static String TITLE = "title";

    LinearLayout suminfolist;
    private String mSuminfoTitle;
    private RecyclerView mRecyclerView;
    private HomeActivity mHomeActivity;

    public static Fragment newInstance(String arg) {
        SumInfoListFragment fragment = new SumInfoListFragment();
        Bundle bundle = new Bundle();
        bundle.putString(TITLE, arg);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        suminfolist = (LinearLayout)inflater.inflate(R.layout.suminfolist, null);
        mHomeActivity = (HomeActivity)getActivity();
        mSuminfoTitle = getArguments().getString(TITLE);
        TextView textView = (TextView)suminfolist.findViewById(R.id.tv_sum_info_list);
        textView.setText(mSuminfoTitle);
        List<SummaryInfo> sumifolist = mHomeActivity.getSummaryInfoList(mSuminfoTitle);
        mRecyclerView = (RecyclerView) suminfolist.findViewById(R.id.recycler_view);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        mRecyclerView.setAdapter(new NormalRecyclerViewAdapter(getActivity(), sumifolist));
        mRecyclerView.setItemAnimator(new DefaultItemAnimator());
        mRecyclerView.addItemDecoration(new DividerItemDecoration(
                getActivity(), DividerItemDecoration.VERTICAL_LIST));

        return suminfolist;
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
    }

    @Override
    public void onDetach() {
        super.onDetach();
    }
}
